#include "MyButton.h"
#include "Common.h"




CMyButton::CMyButton(void)
{	
	m_pDDSButton = NULL;
	m_CommandId = -1;
	m_bDown = false;
}

CMyButton::~CMyButton(void)
{
	if(m_pDDSButton)
		m_pDDSButton->Release();
}

void CMyButton::SetPosition( int x, int y )
{
	m_rButton.SetRect(x, y, x + BUTTON_WIDTH, y + BUTTON_HEIGHT);
}

void CMyButton::SetText( LPCTSTR lpszText )
{
	m_sText = lpszText;
	Init();
}

bool CMyButton::CreateButton( LPDIRECTDRAW pDD )
{
	DDSURFACEDESC ddsd;
	memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
	ddsd.dwWidth = BUTTON_WIDTH;
	ddsd.dwHeight = BUTTON_HEIGHT;
	HRESULT hRet = pDD->CreateSurface( &ddsd, &m_pDDSButton,  NULL );
	if (hRet != DD_OK)
		return false;

	Init();

	return true;
}

void CMyButton::Init()
{
	CCommon::ClearDxSurface(m_pDDSButton);

	HDC hdc;
	m_pDDSButton->GetDC(&hdc);
#ifdef _I8_MID_SDK_
	CRect rDest(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
#else
	CMzRect rDest(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
#endif
	//DrawRect(hdc, rDest, RGB(221, 221, 221), RGB(43, 43, 43));
	if(!m_bDown)
		DrawRect(hdc, rDest, RGB(94, 93, 65), RGB(50, 49, 45));
	else
		DrawRect(hdc, rDest, RGB(184, 166, 94), RGB(67, 58, 43));

	HFONT hfont = FontHelper::GetFont(25);
	HFONT hOldFont = (HFONT)SelectObject(hdc, hfont);
	int bkMode = SetBkMode(hdc, TRANSPARENT);
	COLORREF color = RGB(255, 255, 255);
	if(!m_bDown)
		color = SetTextColor(hdc, RGB(186, 156, 84));
	else
		color = SetTextColor(hdc, RGB(225, 198, 83));

#ifdef _I8_MID_SDK_
	CRect rcText(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
#else
	CMzRect rcText(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
#endif

	DrawText(hdc, m_sText, m_sText.Length(), rcText, DT_CENTER | DT_VCENTER);
	SelectObject(hdc, hOldFont);
	m_pDDSButton->ReleaseDC(hdc);
}

void CMyButton::Paint( LPDIRECTDRAWSURFACE pBackBuffer )
{
	HRESULT hRet = pBackBuffer->Blt(m_rButton, m_pDDSButton, NULL, DDBLT_WAITNOTBUSY, NULL);
}

BOOL CMyButton::HitTest( int xPos, int yPos )
{
#ifdef _I8_MID_SDK_
	CPoint currentPoint(xPos, yPos);
#else
	CMzPoint currentPoint(xPos, yPos);
#endif
	return m_rButton.PtInRect(currentPoint);
}

void CMyButton::Down( bool val )
{
	m_bDown = val;
	Init();
}
